package com.foreveross.bsl.common.utils.web.model;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.util.Assert;

/**
 * 针对mustache的分页数据模型，适配Page类
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-7-5
 *
 */
public class PageMustache {
	private Boolean hasPreviousPage;
	private Boolean hasNextPage;
	private int current;
	private int prevPage, nextPage;
	private int totalPage;
	private long totalElements;
	private int begin, end;
	private PageIndex[] indexs;

	public class PageIndex {
		private int index = 1;
		private Boolean currentIndex;

		public PageIndex() {
		}

		public PageIndex(int index) {
			this.index = index;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public Boolean getCurrentIndex() {
			this.currentIndex = index == current;
			return currentIndex;
		}

	}

	 /**
     * 方便没有DbRef而需要连表操作时,没有pageable的list使用分页(配合showlist()使用)
     * @param list
     * @param pageNumber
     * @param pageSize
     */
	public PageMustache(int totalElements,int pageNumber,int pageSize) {
		if(pageSize<=0){
			pageSize=5;
		}
		this.totalPage = (totalElements-1)/pageSize+1;
		this.current = pageNumber + 1;
		this.begin = Math.max(1, current - pageSize / 2);//begin,end(开始,结束页码),显示页码数: pageSize/2
		this.end = Math.min(begin + (pageSize - 1), totalPage);
		
		
		this.totalElements = totalElements;
		this.hasPreviousPage = current>1?true:false;
		if (this.hasPreviousPage) {
			this.prevPage = current - 1;
		}
		this.hasNextPage = current<totalPage?true:false;
		if (this.hasNextPage) {
			this.nextPage = current + 1;
		}
		this.buildIndexs();
	}
	/**
	 * 获取某页应该显示的list数据
	 * @param list
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public static List<?> showList(List<?> list,int pageNumber,int pageSize){
		List<?> showList=list.subList(pageNumber*pageSize, Math.min((pageNumber+1)*pageSize, list.size()));
		return showList;
	} 
	
	public PageMustache(Page<?> page, int paginationSize) {
		Assert.notNull(page, "page param can not be null");
		if(paginationSize<=0){
			paginationSize=5;
		}
		this.current = page.getNumber() + 1;
		this.begin = Math.max(1, current - paginationSize / 2);
		this.end = Math.min(begin + (paginationSize - 1), page.getTotalPages());
		this.totalPage = page.getTotalPages();
		this.totalElements = page.getTotalElements();
		this.hasPreviousPage = page.hasPreviousPage();
		if (this.hasPreviousPage) {
			this.prevPage = current - 1;
		}
		this.hasNextPage = page.hasNextPage();
		if (this.hasNextPage) {
			this.nextPage = current + 1;
		}
		this.buildIndexs();
	}

	private void buildIndexs() {
		indexs = new PageIndex[end - begin + 1];
		int n=0;
		for (int i = begin; i <= end; i++) {
			indexs[n++] = new PageIndex(i);
		}
	}

	public Boolean getHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(Boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public Boolean getHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(Boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * @return the totalElements
	 */
	public long getTotalElements() {
		return totalElements;
	}

	/**
	 * @param totalElements the totalElements to set
	 */
	public void setTotalElements(long totalElements) {
		this.totalElements = totalElements;
	}

}
